package com.hyjr.common.utils;

import java.util.ResourceBundle;

import org.apache.log4j.Logger;

/**
 * 
 * @ClassName: LogUtils
 * @Description: 记录log
 * @author qianji
 * @date 2015-4-9 上午10:46:34
 *
 */
public class LogUtils {

	/** 接口层 API 日志 **/
	protected static final Logger SERVICE_API_LOG = Logger.getLogger("api");

	protected static final Logger SYNC_DATA_LOG = Logger.getLogger("sync");

	/**
	 * 数据日志
	 */
	protected static final Logger DATA_LOG = Logger.getLogger("data");

	/**
	 * 服务日志
	 */
	protected static final Logger SERVICE_LOG = Logger.getLogger("service");

	protected static final Logger MANGAE_LOG = Logger.getLogger("manage");

	/**
	 * 调式日志，
	 */
	protected static final Logger DEBUG_LOG = Logger.getLogger("debug");

	/**
	 * 未被处理，被系统捕获的例外
	 */
	protected static final Logger EXCEPTION_LOG = Logger.getLogger("exception");

	protected static final Logger SCHEDUL_LOG = Logger.getLogger("schedul");

	/**
	 * 用户访问日志
	 */
	protected static final Logger USER_LOG = Logger.getLogger("user");

	private static boolean open = false;

	private static String key = "log4j.appender.debug.open";

	private static ResourceBundle rb;

	static {
		rb = ResourceBundle.getBundle("log4j");
		open = Boolean.parseBoolean(rb.getString(key));
	}

	public static void LogAll(Logger log, Object... items) {
		if (null == log)
			log = DEBUG_LOG;
		if (null == items || items.length == 0)
			return;
		StringBuffer l = new StringBuffer();
		for (Object item : items) {
			l.append(null == item ? "" : item.toString());
		}
		log.info(l.toString());
	}

	public static void syncLog(String message) {
		SYNC_DATA_LOG.info(message);
	}

	public static void serviceLog(String message) {
		SERVICE_LOG.info(message);
	}

	/**
	 * service日志
	 * 
	 * @param message
	 */
	public static void manageLog(String message) {
		MANGAE_LOG.info(message);
	}

	public static void errorLog(String message, Throwable e) {
		EXCEPTION_LOG.error(message, e);
	}

	public static void error(String message) {
		EXCEPTION_LOG.error(message);
	}

	public static void schedul(String message, Throwable e) {
		SCHEDUL_LOG.info(message, e);
	}

	/**
	 * 调式日志
	 * 
	 * @param message
	 */
	public static void debug(String message) {
		if (open) {
			DEBUG_LOG.debug(message);
		}
	}

	public static void debug(String message, Throwable e) {
		DEBUG_LOG.debug(message, e);
	}

	public static void userLog(String log) {
		USER_LOG.info(log);
	}

	public static void dataLog(String log) {
		DATA_LOG.info(log);
	}

	public static void serviceApiLog(String log) {
		SERVICE_API_LOG.info(log);
	}

}
